#include <sys/mman.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
using namespace std;

int main()
{
    /* 获取文件大小 */
    const char *filename = "./tongxin.txt";
    int fd = open(filename, O_RDWR);
    if (fd == -1)
    {
        perror("open error:");
        exit(-1);
    }

    off_t length = lseek(fd, 0, SEEK_END);

    void *address = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (address == MAP_FAILED)
    {
        perror("mmap error:");
        exit(-1);
    }

  
    /* 2.向文件中写数据：helloworld */
    const char *data = "helloworld";
    int writeBytes = 0;
    while (1)
    {
        writeBytes = write(fd, (const void *)data, strlen(data));
        cout << "writeBytes:" << writeBytes << endl;
        /* 休眠1s */
        sleep(1);
    }
    /* 3.关闭文件 */
    close(fd);
    return 0;
}